package com.cidp.system.service.impl;

import java.util.regex.Pattern;

import com.cidp.system.common.Constants;
import com.cidp.system.dao.IOperatorDetailDao;
import com.cidp.system.model.Toperator;
import com.cidp.system.model.ToperatorDetail;
import com.cidp.system.service.IOperatorDetailService;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.RegexUtils;

public class OperatorDetailServiceImpl implements IOperatorDetailService {
	
	private IOperatorDetailDao operatorDetailDao;
	
	public void setOperatorDetailDao(IOperatorDetailDao operatorDetailDao) {
		this.operatorDetailDao = operatorDetailDao;
	}

	/**
     * 根据对象删除
     * @param ToperatorDetail 对象
     */
	public void delete(ToperatorDetail obj) throws BusiException {
		operatorDetailDao.deleteObject(obj);

	}

	/**
	 * 读取操作员详细信息
	 * @param seNo 序号
	 * @return ToperatorDetail对象
	 */
	public ToperatorDetail load(Long seNo) throws BusiException {
		if(seNo==null && Utility.isEmpty(seNo.toString())){
			throw new BusiException("序号为空字段！");
		}
		return (ToperatorDetail)operatorDetailDao.loadObject(ToperatorDetail.class, seNo);
		
	}
	/**
     * 根据对象保存.
     * @param ToperatorDetail 对象
     * @return Long 主键值
     */
	public Long save(ToperatorDetail obj) throws BusiException {
		if(obj.getLoginId()==null || Utility.isEmpty(obj.getLoginId())){
			throw new BusiException("登陆ID为空字段！");
		}
		validate(obj);
		return operatorDetailDao.saveObject(obj);
	}
	/**
     * 根据对象更新
     * @param ToperatorDetail 对象
     */
	public void update(ToperatorDetail obj) throws BusiException {
		if(obj.getLoginId()==null || Utility.isEmpty(obj.getLoginId())){
			throw new BusiException("登陆ID为空字段！");
		}
		validate(obj);
		operatorDetailDao.updateObject(obj);
	}
	/**
	 * 删除记录
	 */
	public void deleteById(Long id)throws BusiException{
		ToperatorDetail operatorDetail=this.load(id);
		operatorDetailDao.deleteObject(operatorDetail);
	}
	
	public void validate(ToperatorDetail obj) throws BusiException{
		if(obj.getMsn()!=null && !Utility.isEmpty(obj.getMsn())){
			boolean isMsn = Pattern.matches(RegexUtils.email_regexp,obj.getMsn());
			if(isMsn==false){
				throw new BusiException("msn不符合规范！");
			}
		}
		if(obj.getQq()!=null && !Utility.isEmpty(obj.getQq())){
			boolean isQq = Pattern.matches("(^[0-9]{5,15}$)",obj.getQq());
			if(isQq==false){
				throw new BusiException("QQ不符合规范！");
			}
		}
		if(obj.getPostNo()!=null && !Utility.isEmpty(obj.getPostNo().toString())){
			System.out.print("ggg="+obj.getPostNo());
			boolean isPost = Pattern.matches(RegexUtils.ZIP_regexp,obj.getPostNo().toString());
			if(isPost==false){
				throw new BusiException("邮编不符合规范！");
			}
		}
		
	}
	public IPageList listByPage(PagePoParam pagePoParam) throws BusiException
	{
		return operatorDetailDao.listByPage(pagePoParam);
	}
	/**
	 * 读取操作员详细信息
	 * @param id 登陆ID
	 * @return ToperatorDetail对象
	 */
	public ToperatorDetail loadById(String id)  throws BusiException{
		return operatorDetailDao.loadById(id);
	}

}
